package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.TalentInfoImportEnum;
import com.ruoyi.common.enums.TalentInfoStateEnum;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.QQSMSUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TalTalentInfoMapper;
import com.ruoyi.system.domain.TalTalentInfo;
import com.ruoyi.system.service.ITalTalentInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 人才信息Service业务层处理
 *
 * @author zhusq
 * @date 2023-03-27
 */
@Service
public class TalTalentInfoServiceImpl extends ServiceImpl<TalTalentInfoMapper,TalTalentInfo> implements ITalTalentInfoService
{
    @Autowired
    private TalTalentInfoMapper talTalentInfoMapper;

    @Autowired
    private ISysUserService userService ;

    @Autowired
    private SysUserRoleMapper userRoleMapper;

    /**
     * 查询人才信息
     *
     * @param id 人才信息主键
     * @return 人才信息
     */
    @Override
    public TalTalentInfo selectTalTalentInfoById(Long id)
    {
        return talTalentInfoMapper.selectTalTalentInfoById(id);
    }

    /**
     * 查询人才信息列表
     *
     * @param talTalentInfo 人才信息
     * @return 人才信息
     */
    @Override
    public List<TalTalentInfo> selectTalTalentInfoList(TalTalentInfo talTalentInfo)
    {
        List<TalTalentInfo> list = talTalentInfoMapper.selectTalTalentInfoList(talTalentInfo);
        for (TalTalentInfo talentInfo : list) {
            talentInfo.setStateDescribe(TalentInfoStateEnum.getMessageByCode(talentInfo.getState()));
            talentInfo.setImportStateDescribe(TalentInfoImportEnum.getMessageByCode(talentInfo.getImportState()));
        }
        return list;
    }

    /**
     * 新增人才信息
     *
     * @param talTalentInfo 人才信息
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertTalTalentInfo(TalTalentInfo talTalentInfo)
    {
        talTalentInfo.setCreateTime(DateUtils.getNowDate());
        talTalentInfo.setUserId(SecurityUtils.getUserId());
        talTalentInfo.setEmail(SecurityUtils.getLoginUser().getUser().getEmail());
        int insert = talTalentInfoMapper.insert(talTalentInfo);
        /**设置用户信息*/
        SysUser sysUser = new SysUser();
        sysUser.setUserId(SecurityUtils.getUserId());
        sysUser.setNickName(talTalentInfo.getName());
        LambdaUpdateWrapper<SysUser> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(SysUser::getUserId ,SecurityUtils.getUserId()) ;
        wrapper.set(SysUser::getUserId ,sysUser.getUserId()) ;
        wrapper.set(SysUser::getNickName ,talTalentInfo.getName()) ;
        userService.update(sysUser ,wrapper) ;
        return insert;
    }

    /**
     * 修改人才信息
     *
     * @param talTalentInfo 人才信息
     * @return 结果
     */
    @Override
    public int updateTalTalentInfo(TalTalentInfo talTalentInfo)
    {
        talTalentInfo.setUpdateTime(DateUtils.getNowDate());
        return talTalentInfoMapper.updateById(talTalentInfo);
    }

    /**
     * 批量删除人才信息
     *
     * @param ids 需要删除的人才信息主键
     * @return 结果
     */
    @Override
    public int deleteTalTalentInfoByIds(Long[] ids)
    {
        return talTalentInfoMapper.deleteTalTalentInfoByIds(ids);
    }

    /**
     * 删除人才信息信息
     *
     * @param id 人才信息主键
     * @return 结果
     */
    @Override
    public int deleteTalTalentInfoById(Long id)
    {
        return talTalentInfoMapper.deleteTalTalentInfoById(id);
    }

    @Override
    public void talentProcess(Long id) {
        LambdaUpdateWrapper<TalTalentInfo> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(TalTalentInfo::getId,id) ;
        wrapper.set(TalTalentInfo::getState,TalentInfoStateEnum.TALENT_TOBE_ADMIN_PROCESS.getCode());
        this.update(wrapper) ;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void adminProcess(Long id) {
        LambdaUpdateWrapper<TalTalentInfo> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(TalTalentInfo::getId,id) ;
        wrapper.set(TalTalentInfo::getState,TalentInfoStateEnum.TALENT_SUCCESS.getCode());
        wrapper.set(TalTalentInfo::getImportState,TalentInfoImportEnum.TALENT_IMPORT.getCode());
        this.update(wrapper) ;
        TalTalentInfo talTalentInfo = this.getById(id);
        /**添加用户角色*/
        ArrayList<SysUserRole> list = new ArrayList<>();
        SysUserRole role = new SysUserRole();
        role.setUserId(talTalentInfo.getUserId());
        role.setRoleId(100L);
        list.add(role) ;
        userRoleMapper.batchUserRole(list) ;
        try {
            QQSMSUtils.noticeTalentEmail(talTalentInfo.getEmail() ,"你的党员申请已经通过!" ,"恭喜你,你的党员申请已经通过");
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("发送通知邮箱失败!") ;
        }
    }
}
